﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetNexusSpam.SpamFilter;
using System.IO;
using Common.Logging;
using System.Reflection;

namespace DotNetNexusSpam
{
    class InboxToWatch
    {
        /// <summary>
        /// Loads all emails that the engine will be watching.
        /// If 'inboxToWatch.txt' is empty, all emails will be watched/filtered.
        /// </summary>
        private ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private const string INBOX_LIST = "inboxToWatch.txt";
        private string[] _blacklist;
        private bool _isEmpty;
        public bool IsEmpty
        {
            get { return _isEmpty; }
            set { _isEmpty = value; }
        }

        public string[] LoadInboxList(string startupPath)
        {
            string configPath = startupPath + "config\\" + INBOX_LIST;
            if (File.Exists(configPath))
            {
                string blackListContent = "";
                using (StreamReader settingsReader = new StreamReader(configPath))
                {
                    blackListContent = settingsReader.ReadToEnd();
                }
                _blacklist = blackListContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                _log.Debug("InboxToWatchList file was loaded succesfully.");
            }
            else
            {
                _log.Warn("InboxToWatchList file could not be loaded from " + configPath);
            }
            if (_blacklist != null && _blacklist.Length > 0)
            {
                bool hasValidElements = false;
                foreach (string domainElement in _blacklist)
                {
                    if (domainElement.Trim() != "")
                    {
                        hasValidElements = true;
                        break;
                    }
                }
                if (hasValidElements)
                    _isEmpty = false;
                else
                    _isEmpty = true;
            }
            else
                _isEmpty = true;
            return _blacklist;
        }
    }
}
